﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using BCCommon;
using BCCommon.TradingEnums;
using BCDto.Trading.SaleContract;
using BCService.Trading.SaleContract;
using BCTMSRESTful.Configs;
using Microsoft.AspNetCore.Mvc;


namespace BCTMSRESTful.Controllers.Anonymous
{
    [Route("api/v1/[controller]")]
    [ApiController]
    public class SaleContractController : WebsiteBaseController
    {
        private readonly ISaleContractService saleContractService;

        public SaleContractController(ISaleContractService saleContractService)
        {
            this.saleContractService = saleContractService;
        }

        #region 添加销售合同
        /// <summary>
        ///  添加销售合同
        /// </summary>
        /// <param name="requestDto">销售合同</param>
        /// <response code="201">添加销售合同成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPost]
        [Log("添加销售合同")]
        [ProducesResponseType(typeof(SaleContractDto), 201)]
        public IActionResult Post(
            [FromBody]SaleContractRequestDto requestDto)
        {
            var result = this.saleContractService.Add(requestDto);
            return StatusCode((int)HttpStatusCode.Created, result);
        }
        #endregion

        #region 获取销售合同列表
        /// <summary>
        ///  获取销售合同列表
        /// </summary>
        /// <response code="200">获取购物车列表</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<SaleContractDto>), 200)]
        public IActionResult List()
        {
            return StatusCode((int)HttpStatusCode.OK, this.saleContractService.GetList(this.UserInfo.UserId));
        }
        #endregion
        #region 获取销售合同（分页）
        /// <summary>
        ///  获取销售合同（分页）
        /// </summary>
        /// <param name="saleContractStatus">销售合同状态</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="sortName">排序字段</param>
        /// <param name="ascending">升序</param>
        /// <param name="index">当前页码</param>
        /// <param name="size">当前页大小</param>
        /// <response code="200">获取销售合同（分页）</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<SaleContractDto>), 200)]
        public IActionResult Query(
            [FromQuery(Name = "sale_contract_status")]SaleContractStatusType? saleContractStatus,
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            [FromQuery(Name = "sort_name")]string sortName,
            [FromQuery(Name = "ascending")]bool? ascending,
            [FromQuery(Name = "index")]int index = 1,
            [FromQuery(Name = "size")]int size = 10)
        {
            return StatusCode((int)HttpStatusCode.OK, this.saleContractService.Query(this.UserInfo.UserId, saleContractStatus, start, end, sortName, ascending, index, size));
        }
        #endregion
    }
}
